package com.zcmob.web.admanagement.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zcmob.web.admanagement.util.PropsUtils;

public class SessionValidateFilter implements Filter {

    Logger logger = LoggerFactory.getLogger("dmzlog");

    List<String> list = new ArrayList<String>();
    
    private static String validpathPatternStr = "((\\.gif)|\\.png)|(\\.jpg)|(ad_login.jsp)|(login)|(ad_regist.jsp)|(regist)|(\\.css)|(\\.js$)";
    private static Pattern validpathPattern = Pattern.compile(validpathPatternStr, Pattern.CASE_INSENSITIVE);
    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    public String getRemortIP(HttpServletRequest request) {

        if (request.getHeader("x-forwarded-for") == null) {

            return request.getRemoteAddr();

        }

        return request.getHeader("x-forwarded-for");

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        String uri= ((HttpServletRequest)request).getRequestURI();
//        String path=uri.substring(uri.lastIndexOf("/") + 1, uri.length());
        Matcher contentidMatcher = validpathPattern.matcher(uri);
        if (contentidMatcher.find()) {
            //如果符合以上pattern的话，直接进login
            chain.doFilter(request, response);
        } else if (((HttpServletRequest)request).getSession().getAttribute("aduser") == null) {
//            chain.doFilter(request, response);
            ((HttpServletResponse)response).sendRedirect("ad_login.jsp");
            
        } else {
            chain.doFilter(request, response);
        }
        
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        
        list.add("login");
        list.add("regist");
        list.add("ad_login.jsp");
        list.add("ad_regist.jsp");
        
        String dir = getClass().getClassLoader().getResource("/").getPath();
        System.out.println(dir);
        try {
            PropsUtils.load2SystemProperties(new File(dir + "env.properties"));
        } catch (IOException e) {
            
            throw new ServletException(e);
        }
    }

    public static void main(String[] args) {
        String line = "/zcmob/upload_app.jsp";
        
        Matcher contentidMatcher = validpathPattern.matcher(line);
        if (contentidMatcher.find()) {
            //如果符合以上pattern的话，直接进login
            System.out.println(contentidMatcher.group());
        }
    }
}
